//
//  WD_RecomViewController.m
//  BUDING
//
//  Created by HanWoonYong on 16/4/12.
//  Copyright © 2016年 HanWoonYong. All rights reserved.
//

#import "WD_RecomViewController.h"
#import "DCPicScrollView.h"
#import "WD_RecomModel.h"
#import "WD_HomeRecomTableViewCell.h"
#import "WD_RecomCollectionViewCell.h"
#import "WD_RecomCollectionReusableView.h"
#import "WD_LittleGirlView.h"
#import "WD_TopRingPictureModel.h"
#import "WD_MyPlayer.h"

@interface WD_RecomViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *topPicture;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, strong)NSMutableArray *recomArray;
@property (nonatomic, strong)NSMutableArray *titleArray;
@property (nonatomic, strong)NSMutableArray *epArray;
@property (nonatomic, copy)NSString *idStr;
@property (nonatomic, copy)NSString *url;
@property (nonatomic, strong)WD_LittleGirlView *littleGirlView;
@property (nonatomic, strong)WD_MyPlayer *player;
@end

@implementation WD_RecomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatCollectionView];
    [self getTopRingPictureData];
    [self getInfo];
    
}

- (void)getTopRingPictureData {
    [JQAFNetTool GETNetWithUrl:@"http://pudding.cc/api/v1/config?fields=recommend_banner&apiKey=yuki_android&deviceKey=ffffffff-aecb-dcd6-ffff-ffffeb6b6ed7&version=2.8.9&brand=samsung&model=SM-G906S&osv=4.4.2&os=Android&cpuArch=armv7l&timestamp=1459304037&channelId=Wandoujia&auth1=4d43ad6b4458d91ffd1ccc9e2b4c1881" body:nil headerFile:nil response:JQJSON success:^(id result) {
        if (result) {
            self.topPicture = [NSMutableArray arrayWithCapacity:0];
            NSArray *array = [result objectForKey:@"recommend_banner"];
            for (NSDictionary *dic in array) {
                WD_TopRingPictureModel *topRingPictureModel = [[WD_TopRingPictureModel alloc] initWithDataSource:dic];
                [self.topPicture addObject:topRingPictureModel.imageUrl];
            }
            
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)getInfo {
    [JQAFNetTool GETNetWithUrl:@"http://pudding.cc/api/v1/channel/recommended_ep?epLimit=4&apiKey=yuki_android&deviceKey=ffffffff-aecb-dcd6-ffff-ffffeb6b6ed7&version=2.8.9&brand=samsung&model=SM-G906S&osv=4.4.2&os=Android&cpuArch=armv7l&timestamp=1459669137&channelId=Wandoujia&auth1=11817bd9f08a9d9dd8ff739d3b7e0fbd" body:nil headerFile:nil response:JQJSON success:^(id result) {
        if (result) {
            self.dataSource = [NSMutableArray arrayWithCapacity:0];
            _titleArray = [NSMutableArray arrayWithCapacity:0];
            _recomArray = [NSMutableArray arrayWithCapacity:0];
            NSMutableArray *array = [NSMutableArray arrayWithArray:result];
            for (NSDictionary *dic in array) {
                WD_RecomModel *recomModel = [[WD_RecomModel alloc] initWithDataSource:dic];
                [self.recomArray addObject:recomModel];
                [self.titleArray addObject:recomModel.name];
                self.epArray = [NSMutableArray arrayWithArray:recomModel.eps];
            }
            [self.collectionView reloadData];
            DCPicScrollView *picture = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, -150, self.view.frame.size.width, 150) WithImageUrls:self.topPicture];
            [self.collectionView addSubview:picture];
        }
        
    } failure:^(NSError *error) {
        
        
        
    }];
    
}



- (void)creatCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 15, HEIGHT / 4);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 100) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.contentInset = UIEdgeInsetsMake(150, 0, 0, 0);
    
    [self.collectionView registerClass:[WD_RecomCollectionViewCell class] forCellWithReuseIdentifier:@"recom"];
    [self.collectionView registerClass:[WD_RecomCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headers"];
    [self.view addSubview:self.collectionView];

}

#pragma mark - CollectionViewDelegate

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    WD_RecomCollectionReusableView *recomReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headers" forIndexPath:indexPath];
    recomReusableView.backgroundColor = [UIColor colorWithWhite:0.745 alpha:1.000];
    recomReusableView.backgroundColor = [UIColor whiteColor];
    recomReusableView.title.text = self.titleArray[indexPath.section];
    return recomReusableView;
}

#pragma mark - 返回分区头尺寸

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(WIDTH, 30);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.titleArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.epArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WD_RecomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recom" forIndexPath:indexPath];
    WD_RecomModel *remodel = self.recomArray[indexPath.section];
    WD_RecomEpsModel *recomEpsModel = remodel.eps[indexPath.item];
    cell.recomEpsModel = recomEpsModel;
    self.idStr = recomEpsModel._id;
    return cell;
}

- (void)changeButtonAction:(UIButton *)sender {
    NSLog(@"点我了");
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    WD_RecomModel *remodel = self.recomArray[indexPath.section];
    WD_RecomEpsModel *recomEpsModel = remodel.eps[indexPath.item];
    NSLog(@"%ld", indexPath.item);
    self.numBlock(indexPath.item);
    self.idBlock(recomEpsModel._id);
    if (self.cellBlock) {
        self.cellBlock();
    }
    self.dontPlayBlock();
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
